package com.zqc.wms.configuration;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.zqc.wms.entity.UserInfo;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class AuthRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        if (token instanceof UsernamePasswordToken) {
            UsernamePasswordToken uToken = (UsernamePasswordToken) token;
            UserInfo userInfo = new UserInfo();
            userInfo.setToken(UUID.fastUUID().toString().replace("-", ""));
            if (StrUtil.equals(uToken.getUsername(), "admin")) {
                userInfo.setName("Super Admin");
                userInfo.setIntroduction("I am a super administrator");
                userInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
                userInfo.getRoles().add("admin");
            } else {
                userInfo.setName("Normal Editor");
                userInfo.setIntroduction("I am an editor");
                userInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
                userInfo.getRoles().add("editor");
            }
            return new SimpleAuthenticationInfo(userInfo, new String(uToken.getPassword()), getName());
        }
        throw new AuthenticationException("用户认证失败");
    }
}